<template>
  <div class="page-container">
    <div class="page-header">
      <button class="back-button" @click="goToHome">
        <span class="icon-arrow-left"></span>
        返回首页
      </button>
      <h1>权限管理</h1>
      <p>管理用户角色和权限分配，控制系统访问权限</p>
    </div>
    
    <div class="permission-container">
      <div class="roles-list">
        <h2>角色列表</h2>
        <div class="roles-card">
          <div v-for="role in roles" :key="role.id" 
               :class="['role-item', { 'active': selectedRoleId === role.id }]"
               @click="selectRole(role.id)">
            <span class="role-name">{{ role.name }}</span>
            <span class="role-desc">{{ role.description }}</span>
            <div class="role-actions">
              <button class="btn-edit" @click.stop="editRole(role.id)">编辑</button>
              <button class="btn-delete" @click.stop="deleteRole(role.id)" 
                      :disabled="role.systemRole">删除</button>
            </div>
          </div>
          
          <button class="add-role-btn" @click="addRole">
            <span class="icon-plus"></span> 添加新角色
          </button>
        </div>
      </div>
      
      <div class="permission-settings">
        <h2 v-if="selectedRole">
          {{ selectedRole.name }} 权限设置
          <span class="role-users">({{ selectedRole.userCount }} 位用户)</span>
        </h2>
        
        <div v-if="!selectedRole" class="no-selection">
          请从左侧选择一个角色进行权限设置
        </div>
        
        <div v-if="selectedRole" class="permission-tree">
          <div v-for="module in permissionModules" :key="module.id" class="permission-module">
            <div class="module-header">
              <div class="module-info">
                <input type="checkbox" 
                       :checked="isModuleChecked(module.id)" 
                       @change="toggleModule(module.id)">
                <label>{{ module.name }}</label>
              </div>
              <span class="module-desc">{{ module.description }}</span>
            </div>
            
            <div class="module-permissions">
              <div v-for="permission in module.permissions" :key="permission.id" class="permission-item">
                <input type="checkbox" 
                       :id="permission.id"
                       :checked="hasPermission(permission.id)" 
                       @change="togglePermission(permission.id)">
                <label :for="permission.id">{{ permission.name }}</label>
                <span class="permission-desc">{{ permission.description }}</span>
              </div>
            </div>
          </div>
          
          <div class="permission-actions">
            <button class="btn-primary" @click="savePermissions">保存权限设置</button>
            <button class="btn-secondary" @click="resetPermissions">重置</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PermissionSettings',
  data() {
    return {
      selectedRoleId: null,
      roles: [
        {
          id: 1,
          name: '超级管理员',
          description: '拥有系统所有权限',
          systemRole: true,
          userCount: 1,
          permissions: [] // 系统角色拥有所有权限，所以这里为空
        },
        {
          id: 2,
          name: '工单管理员',
          description: '管理工单相关操作',
          systemRole: false,
          userCount: 3,
          permissions: [1, 2, 3, 4, 5, 6]
        },
        {
          id: 3,
          name: '普通员工',
          description: '处理工单但无管理权限',
          systemRole: false,
          userCount: 15,
          permissions: [1, 2, 3]
        }
      ],
      permissionModules: [
        {
          id: 1,
          name: '工单管理',
          description: '工单相关操作权限',
          permissions: [
            { id: 1, name: '查看工单', description: '查看所有工单列表和详情' },
            { id: 2, name: '创建工单', description: '创建新工单' },
            { id: 3, name: '编辑工单', description: '编辑工单内容' },
            { id: 4, name: '分配工单', description: '将工单分配给其他用户' },
            { id: 5, name: '关闭工单', description: '标记工单为已完成' },
            { id: 6, name: '删除工单', description: '从系统中删除工单' }
          ]
        },
        {
          id: 2,
          name: '用户管理',
          description: '用户相关操作权限',
          permissions: [
            { id: 7, name: '查看用户', description: '查看系统用户列表' },
            { id: 8, name: '创建用户', description: '添加新用户' },
            { id: 9, name: '编辑用户', description: '修改用户信息' },
            { id: 10, name: '删除用户', description: '从系统中删除用户' }
          ]
        },
        {
          id: 3,
          name: '系统设置',
          description: '系统配置相关权限',
          permissions: [
            { id: 11, name: '查看系统设置', description: '查看系统配置' },
            { id: 12, name: '修改系统设置', description: '修改系统配置参数' },
            { id: 13, name: '数据备份', description: '执行数据备份操作' },
            { id: 14, name: '权限管理', description: '管理用户角色和权限' }
          ]
        }
      ]
    }
  },
  computed: {
    selectedRole() {
      return this.roles.find(role => role.id === this.selectedRoleId) || null;
    },
    allPermissions() {
      return this.permissionModules.reduce((acc, module) => {
        return [...acc, ...module.permissions];
      }, []);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    selectRole(roleId) {
      this.selectedRoleId = roleId;
    },
    hasPermission(permissionId) {
      // 超级管理员拥有所有权限
      if (this.selectedRole && this.selectedRole.systemRole) {
        return true;
      }
      return this.selectedRole && this.selectedRole.permissions.includes(permissionId);
    },
    togglePermission(permissionId) {
      if (this.selectedRole.systemRole) return; // 系统角色不能修改权限
      
      const index = this.selectedRole.permissions.indexOf(permissionId);
      if (index > -1) {
        this.selectedRole.permissions.splice(index, 1);
      } else {
        this.selectedRole.permissions.push(permissionId);
      }
    },
    isModuleChecked(moduleId) {
      const module = this.permissionModules.find(m => m.id === moduleId);
      if (!module) return false;
      
      // 检查模块下所有权限是否都被选中
      return module.permissions.every(permission => 
        this.hasPermission(permission.id)
      );
    },
    toggleModule(moduleId) {
      if (this.selectedRole.systemRole) return; // 系统角色不能修改权限
      
      const module = this.permissionModules.find(m => m.id === moduleId);
      if (!module) return;
      
      const allChecked = this.isModuleChecked(moduleId);
      
      // 如果全选则取消所有权限，否则选中所有权限
      module.permissions.forEach(permission => {
        const index = this.selectedRole.permissions.indexOf(permission.id);
        if (allChecked && index > -1) {
          this.selectedRole.permissions.splice(index, 1);
        } else if (!allChecked && index === -1) {
          this.selectedRole.permissions.push(permission.id);
        }
      });
    },
    savePermissions() {
      this.$message.success('权限设置已保存');
    },
    resetPermissions() {
      // 这里应该从服务器重新获取原始权限设置
      this.$message.info('已重置为当前保存的权限设置');
    },
    addRole() {
      const newId = Math.max(...this.roles.map(r => r.id)) + 1;
      const newRole = {
        id: newId,
        name: '新角色',
        description: '请编辑角色描述',
        systemRole: false,
        userCount: 0,
        permissions: []
      };
      this.roles.push(newRole);
      this.selectedRoleId = newId;
      this.$message.info('请编辑新角色信息和权限');
    },
    editRole(roleId) {
      // 这里应该打开编辑角色对话框
      const role = this.roles.find(r => r.id === roleId);
      this.$message.info(`正在编辑角色: ${role.name}`);
    },
    deleteRole(roleId) {
      if (confirm('确定要删除这个角色吗？')) {
        this.roles = this.roles.filter(role => role.id !== roleId);
        if (this.selectedRoleId === roleId) {
          this.selectedRoleId = null;
        }
        this.$message.success('角色已删除');
      }
    }
  },
  mounted() {
    // 默认选中第一个角色
    if (this.roles.length > 0) {
      this.selectedRoleId = this.roles[0].id;
    }
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 30px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1e5e8;
}

.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}

h1 {
  margin: 15px 0 10px;
  color: #2c3e50;
}

.page-header p {
  color: #7f8c8d;
  margin: 0;
}

.permission-container {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 20px;
}

.roles-list, .permission-settings {
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

h2 {
  margin-top: 0;
  color: #34495e;
  font-size: 18px;
  border-bottom: 1px solid #f1f1f1;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

.role-item {
  padding: 15px;
  border-radius: 6px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid #f1f1f1;
}

.role-item:hover {
  background-color: #f8f9fa;
}

.role-item.active {
  background-color: #ebf5fb;
  border-color: #3498db;
}

.role-name {
  font-weight: 600;
  color: #2c3e50;
  display: block;
  margin-bottom: 5px;
}

.role-desc {
  font-size: 13px;
  color: #7f8c8d;
  display: block;
  margin-bottom: 10px;
}

.role-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.btn-edit, .btn-delete {
  padding: 3px 8px;
  border-radius: 3px;
  font-size: 12px;
  cursor: pointer;
  border: none;
}

.btn-edit {
  background-color: #3498db;
  color: white;
}

.btn-delete {
  background-color: #e74c3c;
  color: white;
}

.btn-delete:disabled {
  background-color: #ec971f;
  cursor: not-allowed;
}

.add-role-btn {
  width: 100%;
  padding: 10px;
  background-color: #f8f9fa;
  border: 1px dashed #bdc3c7;
  border-radius: 6px;
  color: #3498db;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s;
}

.add-role-btn:hover {
  background-color: #ebf5fb;
  border-color: #3498db;
}

.no-selection {
  text-align: center;
  padding: 50px 0;
  color: #7f8c8d;
  border: 1px dashed #bdc3c7;
  border-radius: 6px;
}

.role-users {
  font-size: 14px;
  font-weight: normal;
  color: #7f8c8d;
  margin-left: 10px;
}

.permission-module {
  margin-bottom: 20px;
  border: 1px solid #f1f1f1;
  border-radius: 6px;
  overflow: hidden;
}

.module-header {
  background-color: #f8f9fa;
  padding: 12px 15px;
  border-bottom: 1px solid #f1f1f1;
}

.module-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}

.module-info label {
  font-weight: 600;
  color: #2c3e50;
}

.module-desc {
  font-size: 13px;
  color: #7f8c8d;
}

.module-permissions {
  padding: 10px 15px;
}

.permission-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid #f9f9f9;
}

.permission-item:last-child {
  border-bottom: none;
}

.permission-item label {
  flex: 1;
  color: #34495e;
}

.permission-desc {
  font-size: 12px;
  color: #7f8c8d;
  white-space: nowrap;
}

.permission-actions {
  margin-top: 30px;
  display: flex;
  gap: 15px;
  justify-content: flex-end;
}

.btn-primary {
  background: #3498db;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background 0.3s;
}

.btn-primary:hover {
  background: #2980b9;
}

.btn-secondary {
  background: #ecf0f1;
  color: #34495e;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-secondary:hover {
  background: #dcdde1;
}
</style>